package com.chat.websocket.util.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * SunJingxuan
 * 2022 \ 08 \ 15
 */

public class DateUtil {

	private static final String PATTEN_DEFAULT_YYYY_MM_DD = "yyyy-MM-dd";

	private static final String PATTEN_DEFAULT_YYYY = "yyyy";
	private static final String PATTEN_DEFAULT_MM_DD = "MM月dd日";

	public static Boolean isToday(String date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTEN_DEFAULT_YYYY_MM_DD);
		String nowString = simpleDateFormat.format(new Date());
		return date.equals(nowString);
	}

	public static Boolean isToday(Date date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTEN_DEFAULT_YYYY_MM_DD);
		return isToday(simpleDateFormat.format(date));
	}

	public static Boolean isYesterday(String date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTEN_DEFAULT_YYYY_MM_DD);
		String yesterdayString = simpleDateFormat.format(new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000));
		return date.equals(yesterdayString);
	}

	public static Boolean isYesterday(Date date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTEN_DEFAULT_YYYY_MM_DD);
		return isYesterday(simpleDateFormat.format(date));
	}

	public static Boolean isThisYear(String date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTEN_DEFAULT_YYYY);
		String thisYear = simpleDateFormat.format(new Date());
		return date.split("-")[0].equals(thisYear);
	}

	public static Boolean isThisYear(Date date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTEN_DEFAULT_YYYY);
		return isThisYear(simpleDateFormat.format(date));
	}

	public static String yearMonthDayToMonthDay(String date) throws ParseException {
		SimpleDateFormat ymd = new SimpleDateFormat(PATTEN_DEFAULT_YYYY_MM_DD);
		SimpleDateFormat md = new SimpleDateFormat(PATTEN_DEFAULT_MM_DD);
		Date paramDate = ymd.parse(date);
		return md.format(paramDate);
	}

	public static void main(String[] args) {
		System.out.println("2021-08-14".substring(5));
	}
}
